package com.nebula.controller.management;

import com.alibaba.fastjson.JSON;
import com.nebula.common.Const;
import com.nebula.common.Result;
import com.nebula.controller.BaseController;
import com.nebula.entity.Navigation;
import com.nebula.service.IPageSettingService;
import com.nebula.service.IRoleNavigationService;
import com.nebula.service.IUserService;
import com.nebula.service.impl.PageSettingServiceImpl;
import com.nebula.service.impl.RoleNavigationServiceImpl;
import com.nebula.service.impl.UserServiceImpl;
import com.nebula.util.UUIDUtil;
import com.nebula.vo.UserVo;
import com.sun.jndi.cosnaming.CNCtx;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author CcccX
 * @since 2020-08-11 22:37
 */
@WebServlet(name = "PageSettingController", urlPatterns = "/manage/pageSetting")
public class PageSettingController extends BaseController {

    private final IPageSettingService pageSettingService = new PageSettingServiceImpl();

    private final IRoleNavigationService roleNavigationService = new RoleNavigationServiceImpl();

    public String index(HttpServletRequest request, HttpServletResponse response) {
        String navId = request.getParameter("id");
        UserVo user = (UserVo) request.getSession().getAttribute(Const.USER_INFO);
        IUserService userService = new UserServiceImpl();
        if (userService.checkNavRole(user.getRoleId(), navId)) {
            return Const.FORWARD + Const.DEFAULT_URL + "website/page-setting.jsp";
        } else {
            return Const.FORWARD + "role-error.jsp";
        }
    }

    /**
     * 获取根节点排序值
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @return String
     */
    public String findRootSort(HttpServletRequest request, HttpServletResponse response) {
        int sort = pageSettingService.selectRootSort();
        Map<String, Integer> map = new HashMap<>(1);
        map.put("sort", sort);
        return Const.JSON + JSON.toJSONString(map);
    }

    /**
     * 获取子节点排序值
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @return String
     */
    public String findChildSort(HttpServletRequest request, HttpServletResponse response) {
        int sort = pageSettingService.selectChildSort();
        Map<String, Integer> map = new HashMap<>(1);
        map.put("sort", sort);
        return Const.JSON + JSON.toJSONString(map);
    }

    public String save(HttpServletRequest request, HttpServletResponse response) {
        String target = request.getParameter("target");
        String sortId = "";
        if ("child".equals(target)) {
            sortId = Const.CHILDREN_SORT_ID;
        } else if ("root".equals(target)) {
            sortId = Const.ROOT_SORT_ID;
        } else if (StringUtils.isBlank(target)) {
            return null;
        }

        //添加页面
        String item = request.getParameter("cname");
        String pid = request.getParameter("pid");
        if ("0".equals(pid)) {
            pid = request.getParameter("cid");
        } else if ("-1".equals(pid)) {
            pid = "0";
        }
        String url = request.getParameter("url");
        String iconCls = request.getParameter("iconCls");
        String sort = request.getParameter("sort");
        String desc = request.getParameter("desc");
        String roleId = request.getParameter("roleId");

        //保存菜单添加权限以及修改排列序号
        Navigation navigation = new Navigation();
        String navId = UUIDUtil.get8UUID();
        navigation.setId(navId);
        navigation.setItem(item);
        navigation.setInstruction(desc);
        navigation.setSort(Integer.parseInt(sort));
        navigation.setUrl(url);
        navigation.setParentId(pid);
        navigation.setIcon(iconCls);
        Date date = new Date();
        navigation.setCreatedTime(date);
        navigation.setUpdatedTime(date);

        try {
            pageSettingService.add(navigation, sortId, sort, roleId, navId);
            return Const.JSON + JSON.toJSONString(Result.createBySuccessMessage("添加成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("添加失败"));
        }
    }

    public String update(HttpServletRequest request, HttpServletResponse response) {
        String cid = request.getParameter("cid");
        String cname = request.getParameter("cname");
        String url = request.getParameter("url");
        String iconCls = request.getParameter("iconCls");
        String desc = request.getParameter("desc");
        String sort = request.getParameter("sort");
        Navigation navigation = new Navigation();
        navigation.setId(cid);
        navigation.setUrl(url);
        navigation.setItem(cname);
        navigation.setIcon(iconCls);
        navigation.setInstruction(desc);
        navigation.setSort(Integer.parseInt(sort));
        try {
            pageSettingService.update(navigation, request.getParameter("roleId"));
            return Const.JSON + JSON.toJSONString(Result.createBySuccessMessage("修改成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("修改失败"));
        }
    }

    public String delete(HttpServletRequest request, HttpServletResponse response) {
        String cid = request.getParameter("id");
        if (StringUtils.isBlank(cid)) {
            return null;
        }

        try {
            //删除菜单
            pageSettingService.deleteNavigationByNavId(cid);
            //删除关系表
            roleNavigationService.deleteByNavId(cid);
            return Const.JSON + JSON.toJSONString(Result.createBySuccessMessage("删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("删除失败"));
        }
    }
}
